﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class D_Dichvu:AccessData
    {
        //-----Hàm lấy ra hết------        
        public DataTable GetAll()
        {
            DataTable dt = new DataTable();
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "Dichvu_Get_List";
            sqlCmd.Parameters.Clear();
            sqlCmd.Connection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
            adapter.Fill(dt);
            sqlCmd.Connection.Close();
            return dt;
        }

        //-----Hàm lấy theo PK DichvuID------
        public M_Dichvu GetbyPK_DichvuID(int DichvuID)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "Dichvu_GetByDichvuId";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.AddWithValue("@DichvuID", DichvuID);
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                M_Dichvu M_Dichvu = null;
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        M_Dichvu = new M_Dichvu();

                        M_Dichvu.DichvuID = Convert.ToInt32(dr["DichvuID"]);
                        if (dr["Giadichvu"] != DBNull.Value)
                            M_Dichvu.Giadichvu = Convert.ToDecimal(dr["Giadichvu"]);
                        if (dr["Mota"] != DBNull.Value)
                            M_Dichvu.Mota = Convert.ToString(dr["Mota"]);
                        if (dr["Ngaydang"] != DBNull.Value)
                            M_Dichvu.Ngaydang = Convert.ToDateTime(dr["Ngaydang"]);
                        if (dr["Tendichvu"] != DBNull.Value)
                            M_Dichvu.Tendichvu = Convert.ToString(dr["Tendichvu"]);                        
                    }
                }
                dr.Close();
                sqlCmd.Connection.Close();
                return M_Dichvu;
            }
        }

        //-----Hàm Insert------
        public void Insert(M_Dichvu M_Dichvu)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "Dichvu_Insert";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add(new SqlParameter("@DichvuID", SqlDbType.Int)).Value = M_Dichvu.DichvuID;
            sqlCmd.Parameters.Add(new SqlParameter("@Tendichvu", SqlDbType.NVarChar, 250)).Value = M_Dichvu.Tendichvu;
            sqlCmd.Parameters.Add(new SqlParameter("@Giadichvu", SqlDbType.Decimal)).Value = M_Dichvu.Giadichvu;
            sqlCmd.Parameters.Add(new SqlParameter("@Mota", SqlDbType.NText)).Value = M_Dichvu.Mota;
            sqlCmd.Parameters.Add(new SqlParameter("@Ngaydang", SqlDbType.DateTime)).Value = M_Dichvu.Ngaydang;            

            sqlCmd.Connection.Open();
            sqlCmd.ExecuteNonQuery();
            sqlCmd.Connection.Close();
        }

        //-----Hàm Update------
        public bool Update(M_Dichvu M_Dichvu)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "Dichvu_Update";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add(new SqlParameter("@DichvuID", SqlDbType.Int)).Value = M_Dichvu.DichvuID;
            sqlCmd.Parameters.Add(new SqlParameter("@Tendichvu", SqlDbType.NVarChar, 250)).Value = M_Dichvu.Tendichvu;
            sqlCmd.Parameters.Add(new SqlParameter("@Giadichvu", SqlDbType.Decimal)).Value = M_Dichvu.Giadichvu;
            sqlCmd.Parameters.Add(new SqlParameter("@Mota", SqlDbType.NText)).Value = M_Dichvu.Mota;
            sqlCmd.Parameters.Add(new SqlParameter("@Ngaydang", SqlDbType.DateTime)).Value = M_Dichvu.Ngaydang;   

            sqlCmd.Connection.Open();
            int ketqua = sqlCmd.ExecuteNonQuery();
            sqlCmd.Connection.Close();
            return (ketqua > 0);
        }

        //-----Hàm xóa------
        public bool Delete(int DichvuID)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "Dichvu_Delete";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add(new SqlParameter("@DichvuID", DichvuID));

            sqlCmd.Connection.Open();
            int ketqua = sqlCmd.ExecuteNonQuery();
            sqlCmd.Connection.Close();
            return (ketqua > 0);
        }
    }
}
